package com.a01_2021.thousandstimes.general.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.general.entity.AbilityModel;
import com.a01_2021.thousandstimes.general.service.IAbilityModelService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-03-31
 */
@RestController
@RequestMapping("/abilityModel")
public class AbilityModelController {

    @Resource
    IAbilityModelService abilityModelService;

    /**
     * 获取全部模型
     *
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('abilityModel-getAllAbilityModel')")
    @DS("#header.instanceid")
    @PostMapping("getAllAbilityModel")
    public AjaxResponse getAllAbilityModel() {
        List<AbilityModel> abilityModelList = abilityModelService.getAllAbilityModel();
        return AjaxResponse.success(abilityModelList);
    }


    /**
     * 通过职位分类id获取能力模型
     *
     * @param postId 职位分类id
     * @return AjaxResponse
     * @throws CustomException 当职位分类不存在时抛出该异常
     */
    @PreAuthorize("hasAuthority('abilityModel-getFromPostId')")
    @DS("#header.instanceid")
    @PostMapping("getFromPostId")
    public AjaxResponse getFromPost(long postId) throws CustomException {
        AbilityModel abilityModel = abilityModelService.getFromPostId(postId);
        return AjaxResponse.success(abilityModel);
    }

    /**
     * 通过职位id获取能力模型
     *
     * @param positionId 职位id
     * @return AjaxResponse
     * @throws CustomException 当职位不存在时抛出该异常
     */
    @PreAuthorize("hasAuthority('abilityModel-getFromPositionId')")
    @DS("#header.instanceid")
    @PostMapping("getFromPositionId")
    public AjaxResponse getFromPositionId(@RequestHeader("instanceid") String instanceId, long positionId) throws CustomException {
        if (instanceId.equals("master")) {
            throw new CustomException("该接口不允许SAAS管理系统访问", 403);
        }
        AbilityModel abilityModel = abilityModelService.getFromPositionId(positionId);
        return AjaxResponse.success(abilityModel);
    }

    /**
     * 更新能力模型
     *
     * @param abilityModel 能力模型
     * @return AjaxResponse
     * @throws CustomException 当能力模型不存在时抛出该异常
     */
    @PreAuthorize("hasAuthority('abilityModel-update')")
    @DS("#header.instanceid")
    @PostMapping("update")
    public AjaxResponse update(AbilityModel abilityModel) throws CustomException {
        AbilityModel updatedAbilityModel = abilityModelService.updateAbilityModel(abilityModel);
        return AjaxResponse.success(updatedAbilityModel);
    }

    /**
     * 通过职位标签id用于获取主数据库中的推荐模型
     *
     * @param postId 职位标签id
     * @return AjaxResponse
     * @throws CustomException 当不存在对应能力模型时抛出该异常
     */
    @PreAuthorize("hasAuthority('abilityModel-getRecommendModel')")
    @DS("master")
    @PostMapping("getRecommendModel")
    public AjaxResponse getRecommendModel(long postId) throws CustomException {
        AbilityModel abilityModel = abilityModelService.getFromPostId(postId);
        return AjaxResponse.success(abilityModel);
    }
}
